home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BBS in a Box 7
/
BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso
/
Files
/
Prog
/
M
/
Lex.cpt
/
Lex
/
CLEX.LXI
< prev
next >
Wrap
Text File
|
1990-04-21
|
4KB
|
182 lines
/*
* C lex
*/
%{
#include <stdlib.h>
#include <console.h>
extern char *install();
char *yyval;
%}
digit = [0-9];
letter = [a-zA-Z_];
name = letter(letter|digit)*;
integer = digit digit*;
%{
main(int argc, char **argv)
{
register int i;
char buffer[80];
extern char *token();
extern FILE * lexin;
#ifdef THINK_C
console_options.nrows = 20;
cecho2file("clex log", 0, stdout);
cecho2file("clex log", 1, stderr);
/* argc = ccommand(&argv); */
lexin = fopen("test.c", "r");
if(lexin == NULL)
Debugger();
#endif
while (i = yylex()) {
gettoken(buffer, sizeof (buffer));
printf("line %2d:yylex returns %3d, token = \"%s\"\n",
yyline, i, *buffer == '\n' ? "\n" : buffer);
if (i == LEXERR) {
error("LEXERR -- abort");
break;
}
}
}
%}
%%
%{
register int c;
%}
#DEFINE { return(__na__); }
#ELSE { return(__na__); }
#ELIF { return(__na__); }
#ENDIF { return(__na__); }
#IFDEF { return(__na__); }
#IFNDEF { return(__na__); }
#LINE {
comment("\n");
return(LEXSKIP);
}
#INCLUDE { return(__na__); }
STRUCT { return(__na__); } /* was: return (-1) */
TYPEDEF { return(__na__); } /* was: return (-1) */
AUTO { return(__na__); }
EXTERN { return(__na__); }
STATIC { return(__na__); }
REGISTER { return(__na__); }
GOTO { return(__na__); }
RETURN { return(__na__); }
IF { return(__na__); }
WHILE { return(__na__); }
ELSE { return(__na__); }
SWITCH { return(__na__); }
CASE { return(__na__); }
BREAK { return(__na__); }
CONTINUE { return(__na__); }
DO { return(__na__); }
DEFAULT { return(__na__); }
FOR { return(__na__); }
SIZEOF { return(__na__); }
TYPEDEF { return(__na__); }
UNION { return(__na__); }
INT { return(__na__); }
CHAR { return(__na__); }
FLOAT { return(__na__); }
LONG { return(__na__); }
SHORT { return(__na__); }
name {
printf("\nmatched name:");
yyval = install();
return(6);
}
integer {
printf("\nmatched integer:");
yyval = install();
return(7);
}
"<" { return(__na__); }
"<=" { return(__na__); }
"=" { return(__na__); }
"!=" { return(__na__); }
">=" { return(__na__); }
">" { return(__na__); }
"<<" { return(__na__); }
">>" { return(__na__); }
"=+" { return(__na__); }
"=-" { return(__na__); }
"=/" { return(__na__); }
"=%" { return(__na__); }
"%" { return(__na__); }
"/" { return(__na__); }
"*" { return(__na__); }
"=*" { return(__na__); }
"=<<" { return(__na__); }
"=>>" { return(__na__); }
"&" { return(__na__); }
"|" { return(__na__); }
"=|" { return(__na__); }
"=&" { return(__na__); }
"+" { return(__na__); }
"-" { return(__na__); }
"++" { return(__na__); }
"--" { return(__na__); }
";" { return(__na__); }
"?" { return(__na__); }
"." { return(__na__); }
"," { return(__na__); }
"/*" {
comment("*/");
return(LEXSKIP);
}
"'" {
if ((c = mapch('\'', '\\')) != -1)
while (mapch('\'', '\\') != -1)
lexerror("Long character constant");
printf("%c", c);
return(__na__);
}
"\"" { return(__na__); } /* This should call "lexswitch" */
"\n" { return(__na__); }
" " { return(__na__); }
"\t" { return(__na__); }
"||" { return(__na__); }
"&&" { return(__na__); }
"(" { return(__na__); }
")" { return(__na__); }
"[" { return(__na__); }
"]" { return(__na__); }
"{" { return(__na__); }
"}" { return(__na__); }
%%
char *
install()
/*
* Install the current token in the symbol table
*/
{
register char *buffer; /* Where to put the character */
register char *first; /* -> first byte of the token */
char *last; /* Can't be in a register */
extern char *token();
extern char *copy();
first = token(&last); /* Find first/last of token */
printf("\tinstalling :%s", first);
if ((buffer = calloc((last - first) + 1, sizeof(char))) == NULL) {
error("Out of space in install");
exit(1);
}
first = copy(buffer, first, (int)(last - first));
*first = '\0';
return(buffer);
}
error(char *s) {
fprintf(stderr, s);
exit(1);
}
char *
copy(char *to, char *from, int len) {
memcpy(to,from,len);
return(&to[len + 1]);
}
void yyinit() {};